//
//  FADeviceCheck.swift
//  flutter_auto_track
//
//  Created by muz on 2024/9/27.
//

import Foundation
import CoreTelephony

public struct FADeviceCheck {
    
    /// 当前设备是否处于安全环境
    public static func currentDeviceIsSafe() -> Bool {
        return !vpnOn() && checkSimIsInstaller();
    }
    
    /// 检测设备有没有越狱
    public static func jailBreak() -> Bool {
        var state = false
        if FileManager.default.fileExists(atPath: "/User/Applications/") {
            state = true
        }
        if let cydiaURL = URL(string: "cydia://"),
           UIApplication.shared.canOpenURL(cydiaURL) {
            state = true
        }
        let jailBreakPathes = ["/Applications/Cydia.app",
                               "/Library/MobileSubstrate/MobileSubstrate.dylib",
                               "/bin/bash",
                               "/usr/sbin/sshd",
                               "/etc/apt"]
        for path in jailBreakPathes {
            if FileManager.default.fileExists(atPath: path) {
                state = true
                break
            }
        }
        return state
    }
    
    /// 检查设备有没有开启vpn
    public static func vpnOn() -> Bool {
        guard let proxy = CFNetworkCopySystemProxySettings() else {
            return false
        }
        let retainedValue = proxy.takeRetainedValue() as NSDictionary
        guard let keys = retainedValue["__SCOPED__"] as? [String:Any] else {
            return false
        }
        let keyValues: [String] = [
            "tap",
            "tun",
            "ppp",
            "ipsec",
        ]
        var result: Bool = false
        for key in keys.keys {
            for value in keyValues {
                if key.contains(value) {
                    result = true
                    break
                }
            }
        }
        return result
    }
    
    /// 检查当前设备是否处于充电状态
    public static func batteryState() -> Bool {
        UIDevice.current.isBatteryMonitoringEnabled = true
        if UIDevice.current.batteryState == .charging || UIDevice.current.batteryState == .full {
            return true
        }
        return false
    }
    
    /// 获取运营商类型
    public static func checkSimIsInstaller() -> Bool {
        var countryCode = ""
        let telephony = CTTelephonyNetworkInfo()
        if #available(iOS 12.0, *) {
            if telephony.serviceCurrentRadioAccessTechnology?.isEmpty ?? true {
                return false
            } else {
                return true
            }
        } else {
            if let cellularProviders = telephony.serviceSubscriberCellularProviders {
                for item in cellularProviders.values {
                    if item.mobileNetworkCode != nil {
                        countryCode = item.mobileCountryCode ?? ""
                        break
                    }
                }
            }
            if countryCode != "460" {
                return false
            }
        }
        return false
    }
}
